Create a new event handler method

When working with a Windows Presentation Foundation (WPF) or Microsoft Silverlight project, Microsoft Expression Blend can generate code for empty event handler methods, to help you get started.

Expression Blend will open your code-behind file, and then paste in the empty event handler method.

For information about event handlers in code or creating interactivity in your Expression Blend application without using code, see Writing code that will respond to events.

To create a new event handler method

  1. Open your project in Expression Blend.

  2. Open your document (for example, MainWindow.xaml) by double-clicking it in the Projects panel.

    Your document will open for editing. Make sure you are in Design view, by clicking the Design tab on the right side of the artboard.

  3. In the Objects and Timeline panel, select the object that you want to hook up to an existing event handler method. For example, if you want a rectangle object to move when a button is clicked, select the button object.

    The background behind the name of the object is highlighted, to show that the object is selected.

  4. In the Properties panel, click EventsCc294821.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(en-us,Expression.40).png.

    A list of all available events for the selected object appears, in alphabetical order.

    Warning

    Events will not appear if you have more than one object selected at the same time in the Objects and Timeline panel (for example, by holding the CTRL key to select multiple objects).

    Tip

    To see a short description of an event, move your pointer over the name of the event. A tooltip will appear, with a description of the event.

  5. Locate the event that you want to add programming logic to. For example, in the rectangle and button example, you would locate the MouseDown event.

  6. There are two ways that you can generate the empty event handler method:

    • Double-click in the text box beside the event name. Expression Blend will generate a default name for your event handler method and enter it into the text box, and will generate the code for the empty method.

    • Type a name into the text box beside the event name, and then press ENTER or click somewhere else to move focus away from the text box. Event method names must begin with a letter. If the method name does not already exist in the code-behind file, Expression Blend will generate the code for the empty method and will use the name that you typed in.

    Expression Blend opens your code-behind file, and then pastes in the empty event handler method for you.

      public partial class Window1
      {
          public Window1()
          {
              this.InitializeComponent();
    
              // Insert code required on object creation below this point.
          }
    
          private void Button_MouseDown(object sender, RoutedEventArgs e)      {      }
      }
    
  7. With your code-behind file open and the event handler method pasted in, you can begin to add code to your method. For the purposes of this procedure, you could add the following line of code in red to make a message box appear when the button is clicked:

      private void Button_MouseDown(object sender, RoutedEventArgs e)
      {
          MessageBox.Show("Hello!");
      }
    

    For examples of event handler methods in code-behind files, see the samples that are available from the Welcome Screen (click Welcome Screen on the Help menu).

    Note

    Expression Blend will give you a build error if you reference an event handler method in the Events panel that does not exist in the code-behind file. If you get this kind of error, you can determine if there is a spelling mistake in the event handler method name, or you can double-click in the text box for the event to create a new empty event handler method.

    When you delete or rename an event handler method name in a text box in the Events panel, Expression Blend does not delete the original method from the code-behind file. This is because the event might still be used elsewhere. You will not receive a build error if there is an event handler method defined in your code-behind file that is not referenced in your XAML file.

    Note

    If you want to reference an object in your .xaml file from a code-behind file, you must name the object in the .xaml file. By default, objects that you create in Expression Blend are not named. You can name an object in the Objects and Timeline panel by right-clicking the object and then clicking Rename.

Send feedback about this topic to Microsoft. © 2011 Microsoft Corporation. All rights reserved.